package com.example.administrator.application

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.support.annotation.RequiresApi
import kotlinx.android.synthetic.main.activity_main.*
import pub.devrel.easypermissions.EasyPermissions
import java.io.File
import java.io.FileInputStream


class MainActivity : BaseActivity(), EasyPermissions.PermissionCallbacks {
    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {

    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {

    }

    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        text.text = "第一个页面"
        val perms = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        if(!EasyPermissions.hasPermissions(this, *perms)) {
            EasyPermissions.requestPermissions(this,"", 1000, *perms)
        }
        text.setOnClickListener {
//            val url = "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201707%2F01%2F191526yd98mlqkzl1aolqd.jpg&thumburl=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D3962030181%2C329894226%26fm%3D26%26gp%3D0.jpg"

            val path = Environment.getExternalStorageDirectory().absolutePath + "/image/test.jpg"
            val file = File(path)
            val stream = FileInputStream(file)
            val bitmap = ImageManager.get().sizeCompress(stream, 200, 200)

//            ImageManager.get().downLoadImage(url, path)
//            val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.test)
            image.setImageBitmap(bitmap)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }
}
